package com.meixiang.dialog;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.adapter.myFundAdapter.ScreenDataAdapter;
import com.meixiang.recyclerview.OnItemClick;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xyj on 2016/5/18.
 * 弹出 popupWindow 层
 */
public class OptionalScreenPopup extends PopupWindow implements View.OnClickListener {

    private LinearLayout ll_default_sort, ll_high_to_low, ll_low_to_high; //默认排序、高到低、低到高
    private TextView tv_default_sort, tv_higt_to_low, tv_low_to_high;
    private RecyclerView screen_swipe_target; //数据列表
    private TextView tv_amount_popup_view_submit; //提交按钮
    private View parentView; //父类属兔
    private Context mContext;
    private Animation showAnimation; //定义一个开始动画
    private Animation disAnimation; //定义一个结束动画
    private int sortType;
    private String selectTitle ="日涨幅";
    private ScreenDataAdapter screenDataAdapter ;
    private PopupWindow.OnDismissListener outDissmissListener;
    private int height;
    private int selectedIndex = 0;
    private IOnSubmitClickListener mSubmitClickListener;
    private Handler handler=new Handler();
    private String[] screenData = {"日涨幅", "近一周", "近一个月", "近三个月", "近六个月", "近一年","今年来"};

    private PopupWindow.OnDismissListener dissmissListener = new PopupWindow.OnDismissListener() {
        public void onDismiss() {
            if (OptionalScreenPopup.this.outDissmissListener != null)
                OptionalScreenPopup.this.outDissmissListener.onDismiss();
            if (OptionalScreenPopup.this.disAnimation != null)
                OptionalScreenPopup.this.parentView.startAnimation(OptionalScreenPopup.this.disAnimation);
        }
    };

    public OptionalScreenPopup(Context context, View parentView, int height) {
        this.parentView = parentView;
        this.mContext = context;
        this.height = height;
        initView();
        initData();
    }

    private void initData() {
        this.setContentView(parentView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(height);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new ColorDrawable(0));
        initScreenData();
        this.showAnimation = new TranslateAnimation(1, 0.0F, 1, 0.0F, 1, -1.0F, 1, 0.0F);
        this.showAnimation.setDuration(500L);
        this.disAnimation = new TranslateAnimation(1, 0.0F, 1, 0.0F, 1, 0.0F, 1, -1.0F);
        this.disAnimation.setDuration(500L);
        this.disAnimation.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationEnd(Animation paramAnonymousAnimation) {
                OptionalScreenPopup.this.tureDismiss();
            }

            public void onAnimationRepeat(Animation paramAnonymousAnimation) {
            }

            public void onAnimationStart(Animation paramAnonymousAnimation) {
            }
        });
    }

    /**
     * 初始化筛选数据
     */
    private void initScreenData() {
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        screen_swipe_target.setLayoutManager(mLayoutManager);
        screen_swipe_target.setItemAnimator(new DefaultItemAnimator());
        screenDataAdapter = new ScreenDataAdapter(mContext, screenData);
        screen_swipe_target.setAdapter(screenDataAdapter);
        screenDataAdapter.setOnItemClickListener(new OnItemClick() {
            @Override
            public void onItemClick(View view, int position) {
                screenDataAdapter.setSelectedIndex(position);
                selectedIndex = position ;
                selectTitle = screenData[position];
                screenDataAdapter.notifyDataSetChanged();
            }
        });
    }

    //选择的回调
    public interface IOnSubmitClickListener {
        void onClick(int selectPos, int rank_type,String titleName);
    }
    /**
     * 初始化视图组件
     */
    private void initView() {
        ll_default_sort = (LinearLayout) parentView.findViewById(R.id.ll_default_sort);
        ll_high_to_low = (LinearLayout) parentView.findViewById(R.id.ll_high_to_low);
        ll_low_to_high = (LinearLayout) parentView.findViewById(R.id.ll_low_to_high);
        tv_default_sort = (TextView) parentView.findViewById(R.id.tv_default_sort);
        tv_higt_to_low = (TextView) parentView.findViewById(R.id.tv_high_to_low);
        tv_low_to_high = (TextView) parentView.findViewById(R.id.tv_low_to_high);
        screen_swipe_target = (RecyclerView) parentView.findViewById(R.id.screen_swipe_target);
        tv_amount_popup_view_submit = (TextView) parentView.findViewById(R.id.tv_amount_popup_view_submit);
        bandOnClickListener();
    }

    /**
     * 绑定点击事件
     */
    public void bandOnClickListener() {
        ll_default_sort.setOnClickListener(this);
        ll_high_to_low.setOnClickListener(this);
        ll_low_to_high.setOnClickListener(this);
        tv_amount_popup_view_submit.setOnClickListener(this);
    }

    public void setSubmitClickListener(IOnSubmitClickListener listener){
        mSubmitClickListener = listener;
    }

    private void tureDismiss() {
        super.dismiss();
    }

    public void setOutDissmissListener(PopupWindow.OnDismissListener paramOnDismissListener) {
        this.outDissmissListener = paramOnDismissListener;
    }

    public void dismiss() {
        this.dissmissListener.onDismiss();
    }

    public void startShowAnimation() {
        if (this.showAnimation != null)
            this.parentView.startAnimation(this.showAnimation);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ll_default_sort:
                sortType = 3;
                tv_default_sort.setCompoundDrawablesRelativeWithIntrinsicBounds(ContextCompat.getDrawable(mContext, R.mipmap.option1), null, null, null);
                tv_low_to_high.setCompoundDrawablesRelativeWithIntrinsicBounds(ContextCompat.getDrawable(mContext, R.mipmap.option2), null, null, null);
                tv_higt_to_low.setCompoundDrawablesRelativeWithIntrinsicBounds(ContextCompat.getDrawable(mContext, R.mipmap.option2), null, null, null);
                break;

            case R.id.ll_high_to_low:
                sortType = 1;
                tv_default_sort.setCompoundDrawablesRelativeWithIntrinsicBounds(ContextCompat.getDrawable(mContext, R.mipmap.option2), null, null, null);
                tv_low_to_high.setCompoundDrawablesRelativeWithIntrinsicBounds(ContextCompat.getDrawable(mContext, R.mipmap.option2), null, null, null);
                tv_higt_to_low.setCompoundDrawablesRelativeWithIntrinsicBounds(ContextCompat.getDrawable(mContext, R.mipmap.option1), null, null, null);
                break;

            case R.id.ll_low_to_high:
                sortType = 2;
                tv_default_sort.setCompoundDrawablesRelativeWithIntrinsicBounds(ContextCompat.getDrawable(mContext, R.mipmap.option2), null, null, null);
                tv_low_to_high.setCompoundDrawablesRelativeWithIntrinsicBounds(ContextCompat.getDrawable(mContext, R.mipmap.option1), null, null, null);
                tv_higt_to_low.setCompoundDrawablesRelativeWithIntrinsicBounds(ContextCompat.getDrawable(mContext, R.mipmap.option2), null, null, null);
                break;

            case R.id.tv_amount_popup_view_submit:
                if (mSubmitClickListener != null) {
                    mSubmitClickListener.onClick(selectedIndex, sortType, selectTitle);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dismiss();
                        }
                    },100);
                }
                break;
        }
    }
}
